package com.tianqu.tms.www.member.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.actionform.MemberInputActionForm;
import com.tianqu.tms.www.member.controller.MemberUDTopListController;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
import com.tianqu.tms.www.member.viewhelper.MemberUDViewHelper;
/**
 * 会员信息修改删除列表页
 * 
 * @author tianshifu
 *
 */
public class MemberUDTopListAction extends TmsAction {

	private static final String MEMLIST = "memList";
	
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";

	private Logger logger = Logger.getLogger(MemberUDTopListAction.class);
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		//从数据库中查询出列表信息,封装到list中迭代
		MemberInputViewHelper view = new MemberInputViewHelper();		
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null){
			uc.removeAttribute(TmsConstants.VIEWHELP);
		}		
		if(request.getParameter(PAGENO) == null) {
			if(uc.getAttribute(PAGENO) == null) {
				uc.setAttribute(PAGENO, view.getPageNo());
				uc.setAttribute(SORTKEY, "9");
				uc.setAttribute(MAXINDEX, "50");
				uc.setAttribute(ASC, "false");
				request.setAttribute(TmsConstants.VIEWHELP, view);
				MemberInputActionForm memberForm = (MemberInputActionForm)uc.getInputForm();
				if ("1".equals(uc.getAttribute("searchFlg"))) {
					uc.removeAttribute("searchFlg");
					if(StringUtil.isNullOrBlank(memberForm.getMemberId()) && StringUtil.isNullOrBlank(memberForm.getMemberStatus())){
						setErrorsToRequest(request, TmsConstants.U0037, null); // 必须输入至少一个查询条件。
						return mapping.getInputForward();
					}
				}	
			}else {
				uc.setAttribute(PAGENO, uc.getAttribute(PAGENO));
				uc.setAttribute(SORTKEY, uc.getAttribute(SORTKEY));
				uc.setAttribute(MAXINDEX, uc.getAttribute(MAXINDEX));
				uc.setAttribute(ASC, uc.getAttribute(ASC));
			}
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		MemberUDTopListController controller = new MemberUDTopListController();
		controller.execute(uc);
		
		view = (MemberInputViewHelper)uc.getAttribute(MEMLIST);
		Member member = (Member)uc.getAttribute(MemberUDTopListController.PAGING);
		if (view.getMemList().size() == 0) {//结果为空
			setErrorsToRequest(request, TmsConstants.U0039, null);
			return mapping.getInputForward();
		}
		// viewhelp中设置翻页。
		view.setPageNo(Integer.toString(member.getPageNo()));
		view.setMaxIndex(Integer.toString(member.getMaxIndex()));
		view.setAsc(Boolean.toString(member.isAsc()));
		view.setSortKey(Integer.toString(member.getSortKey()));
		view.setTotalCount(Integer.toString(member.getTotalCount()));
		view.setDateFrom(Integer.toString(member.getDateFrom()));
		view.setDateTo(Integer.toString(member.getDateTo()));
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
